package com.lemon.smartsafety.ui.adapter.main;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.GradientDrawable;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.AppService;
import com.lemon.smartsafety.app.Constants;
import com.lemon.smartsafety.app.EventTag;
import com.lemon.smartsafety.data.app.DataUtil;
import com.lemon.smartsafety.entity.event.main.MainPanelButtonEvent;
import com.lemon.smartsafety.entity.main.MainPanelButton;
import com.lemon.smartsafety.entity.main.MainRcvItemBase;
import com.lemon.smartsafety.ui.adapter.common.RcvViewType;
import com.lemon.smartsafety.utils.common.LoggerHelper;
import com.lemon.smartsafety.utils.common.ViewUtils;

import java.util.List;

import butterknife.ButterKnife;
import cn.bingoogolapple.badgeview.BGABadgeFrameLayout;
import kale.adapter.item.AdapterItem;

/**
 * Created by Cooliean on 16/2/1.
 */
public class MainOptionItemView implements AdapterItem<MainRcvItemBase<List<MainPanelButton>>> {
    private LoggerHelper loggerHelper = new LoggerHelper(MainOptionItemView.class);

    private GridView mGridView;



    private int mainPanelColumnNumber = Constants.MAIN_PANEL_ICON_COUNT;

    @Override
    public int getLayoutResId() {
        return R.layout.item_scroll_gridview;
    }

    @Override
    public void bindViews(View root) {
        mGridView = (GridView) root.findViewById(R.id.gridView);
        ButterKnife.bind(this, root);
    }

    @Override
    public void setViews() {
    }

    @Override
    public void handleData(MainRcvItemBase<List<MainPanelButton>> mainRcvItemBase, int position) {
        if (RcvViewType.MainRcvViewType.TYPE_MAIN_OPTION != mainRcvItemBase.getViewType()) {
            return;
        }
        int                         screenWidth = ViewUtils.getScreenWidth(mGridView.getContext());
        int                         height      = (int) (screenWidth / mainPanelColumnNumber) - 1;
        final List<MainPanelButton> items       = mainRcvItemBase.getData();
        GridViewAdapter             adapter     = new GridViewAdapter(mGridView.getContext(), items);
        mGridView.setNumColumns(mainPanelColumnNumber);
        mGridView.setAdapter(adapter);
        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {

//                MainPanelButtonEvent event = new MainPanelButtonEvent();
//                event.setTag(EventTag.TAG_MAIN_PANEL_BUTTON_EVENT);
//                event.setIndex(position);
//                event.setButton(items.get(position));
//                AppService.getBus().post(event);

            }
        });
        mGridView.setLayoutParams(new RecyclerView.LayoutParams(screenWidth, height * 2));

    }


    /**
     * 主界面的按钮视图Adapter
     */
    class GridViewAdapter extends BaseAdapter {

        private Context               mContext;
        private List<MainPanelButton> items;


        public GridViewAdapter(Context context, List<MainPanelButton> items) {
            this.mContext = context;
            this.items = items;

        }

        @Override
        public int getCount() {
            return mainPanelColumnNumber * 2;
        }

        @Override
        public Object getItem(int position) {
            return DataUtil.getMainUIButtons().get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            int        width = (int) (parent.getWidth() / mainPanelColumnNumber) - 1;
            if (convertView == null) {
                convertView = View.inflate(mContext, R.layout.item_main_panel, null);
                /*根据parent动态设置convertview的大小*/
                setWH(convertView, parent);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
                /*解决动态设置convertview大小，第一项不显示的BUG*/
                if (position == 0) {
                    setWH(convertView, parent);
                }
                //setWH(convertView, parent);
            }
            MainPanelButton item = items.get(position);

            //这里是根据ICON的大小来确定背景的大小
            int iconHeight = ViewUtils.dip2px(this.mContext, 25) + getImageWH(item.getIconRes())[0];

//            int iconHeight = width - ViewUtils.dip2px(this.mContext, 25) - this.mContext.getResources().getDimensionPixelSize(R.dimen.small_text_size);


            holder.layoutIcon.setLayoutParams(new LinearLayout.LayoutParams(iconHeight, iconHeight));
            GradientDrawable myGrad = (GradientDrawable) holder.layoutIcon.getBackground();
            myGrad.setColor(this.mContext.getResources().getColor(item.getColorRes()));

            holder.iconImage.setImageResource(item.getIconRes());
            holder.textView.setText(mContext.getText(item.getTitleRes()));

            if (item.getBadgeCount() <= 0) {
                holder.layoutIcon.hiddenBadge();
            } else {
                holder.layoutIcon.showTextBadge(item.getBadgeCount() + "");
            }

            //0位置有bug 因为动态设置高度引起的
            if (position == 0 || position > 6) {
                holder.ripple.setEnabled(false);
            } else {
                holder.ripple.setEnabled(true);
            }

            holder.ripple.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    MainPanelButtonEvent event = new MainPanelButtonEvent();
                    event.setTag(EventTag.TAG_MAIN_PANEL_BUTTON_EVENT);
                    event.setIndex(position);
                    event.setButton(items.get(position));
                    AppService.getBus().post(event);
                }
            });
        /*    holder.ripple.setOnRippleCompleteListener(new RippleView.OnRippleCompleteListener() {
                @Override
                public void onComplete(RippleView rippleView) {
                    MainPanelButtonEvent event = new MainPanelButtonEvent();
                    event.setTag(EventTag.TAG_MAIN_PANEL_BUTTON_EVENT);
                    event.setIndex(position);
                    event.setButton(items.get(position));
                    AppService.getBus().post(event);
                }
            });*/

            return convertView;
        }

        private void setWH(View view, ViewGroup parent) {
            view.setLayoutParams(new AbsListView.LayoutParams((int) (parent.getWidth() / mainPanelColumnNumber) - 1, (int) (parent.getWidth() / mainPanelColumnNumber) - 1));// 动态设置item的高度
        }


        class ViewHolder {
            LinearLayout ripple;
//            LinearLayout         mainLayout;
            BGABadgeFrameLayout  layoutIcon;
            ImageView            iconImage;
            TextView             textView;

            ViewHolder(View convertView) {
                ripple = (LinearLayout) convertView.findViewById(R.id.ripple);
//                mainLayout = (LinearLayout) convertView.findViewById(R.id.mainLayout);
                layoutIcon = (BGABadgeFrameLayout) convertView.findViewById(R.id.layoutIcon);
                iconImage = (ImageView) convertView.findViewById(R.id.iconImage);
                textView = (TextView) convertView.findViewById(R.id.textView);
            }
        }

        public int[] getImageWH(int Res) {
            Bitmap bitmap = BitmapFactory.decodeResource(this.mContext.getResources(), Res);
            int    height = bitmap.getHeight();
            int    width  = bitmap.getWidth();
            bitmap = null;
            int[] result = {width, height};
            return result;
        }
    }


}
